clear all

cd "C:\Users\Public\Documents\ImmPanelRevis19\"

/* share immigrants in CZE */
use "ImmPanelRevis19\Donnees\pit\instru2017R21.dta", clear
egen ize=group(ze)
egen time=group(an)
xtset ize time
gen fzeimmig=F.zeimmig
keep an ze zeimmig mlsnd zepop dimm dimm75 fzeimmig
rename ze aze
rename zeimmig azeimmig
rename fzeimmig afzeimmig
rename mlsnd amlsnd 
rename dimm adimm
rename dimm75 adimm75
rename zepop azepop
save tempAZE.dta, replace

cd "C:\Users\Public\Documents\ImmPanelRevis19\"
use "ImmPanelRevis19\donnees\cgeo_ze.dta", clear
drop zen
rename cgeo cgeor
rename ze zer
save cgeor_zer.dta, replace
save "ImmPanelRevis19\donnees\cgeor_zer.dta", replace

use "ImmPanelRevis19\donnees\indiv.dta", clear
/***************************************************/
/* Construct the table final.dta for the estimates */
/***************************************************/
/**************************************/
/* Definition des zones geographiques */
/**************************************/
/**************************************/
/* 1) => Merge avec les ZE 			  */
/**************************************/
/* elimine la corse */
drop if dep=="2A" | dep=="2B"

gen cgeo=dep+comt
sort cgeo
/* Paris */
replace cgeo="75056" if dep=="75"
/* Lyon */
replace cgeo="69123" if cgeo=="69381" | cgeo=="69382" | cgeo=="69383" | cgeo=="69384" | cgeo=="69385" | cgeo=="69386" | cgeo=="69387" | cgeo=="69388" | cgeo=="69389" 
/* Marseille */
replace cgeo="13055" if cgeo=="13201" | cgeo=="13202" | cgeo=="13203" | cgeo=="13204" | cgeo=="13205" | cgeo=="13206" | cgeo=="13207" | cgeo=="13208" | cgeo=="13209" | cgeo=="13210" | cgeo=="13211" | cgeo=="13212" | cgeo=="13213" | cgeo=="13214" | cgeo=="13215" | cgeo=="13216"

/* definit les ZE */
joinby cgeo using "ImmPanelRevis19\donnees\cgeo_ze.dta", unm(m)
tab _merge
drop if _merge==1
drop _merge
drop zen

/* zone d'emploi de la zone d'habitation */
gen cgeor=depr+comr
sort cgeor
/* Paris */
replace cgeor="75056" if depr=="75"
/* Lyon */
replace cgeor="69123" if cgeor=="69381" | cgeor=="69382" | cgeor=="69383" | cgeor=="69384" | cgeor=="69385" | cgeor=="69386" | cgeor=="69387" | cgeor=="69388" | cgeor=="69389" 
/* Marseille */
replace cgeor="13055" if cgeor=="13201" | cgeor=="13202" | cgeor=="13203" | cgeor=="13204" | cgeor=="13205" | cgeor=="13206" | cgeor=="13207" | cgeor=="13208" | cgeor=="13209" | cgeo=="13210" | cgeo=="13211" | cgeo=="13212" | cgeo=="13213" | cgeo=="13214" | cgeo=="13215" | cgeo=="13216"

/* definit les ZE */
joinby cgeor using "ImmPanelRevis19\donnees\cgeor_zer.dta", unm(m)
tab _merge
drop _merge

/* foreign born workers */
gen fborn=(depnai=="99")
gen ifborn="N" if fborn==0
replace ifborn="FB" if fborn==1
drop if fborn

/*********************/
/* add task contents */
/*********************/
joinby cs2h using "ImmPanelRevis19\Donnees\Onet\CS2_OnetNorm.dta", unm(m)
tab _merge
drop _merge

save temp.dta, replace

/***************************/
/* Add contemporary groups */
/***************************/
use "ImmPanelRevis19\donnees\ident_groupmw.dta", clear
rename gref an
save gref.dta, replace

use temp.dta, clear
joinby nninouv an using gref.dta, unm(m)
tab _merge
drop _merge

egen time=group(an)
xtset indiv time
compress

save temp.dta, replace

/* define our group of interest age 25-50 */
keep if ((age>=25) & (age<=50))

drop if an==2007
keep nninouv
contract nninouv 
drop _freq
gen treat=1
save treat.dta, replace

/* only keep 'treated' individuals */
use temp.dta, clear
joinby nninouv using treat.dta, unm(m)
drop if missing(treat)
drop treat

/* define valid first year observation */
gen ftreat=((age>=25) & (age<=50)) 

save temp2.dta, replace

/* Panel A : 76-82 */
use temp2.dta, clear
/* Balanced sample: treated in first & second period */
keep if an==1976 | an==1982
keep if (ftreat==1 & an==1976 & !missing(F.an)) | (L.ftreat==1 & an==1982)
tab an
gen nind="A"+nninouv
egen btime=group(an)
save tempA.dta, replace

/* Panel B : 82-91 */
use temp2.dta, clear
/* Balanced sample: treated in first & second period */
keep if an==1982 | an==1991
keep if (ftreat==1 & an==1982 & !missing(F.an)) | (L.ftreat==1 & an==1991)
tab an
gen nind="B"+nninouv
egen btime=group(an)
save tempB.dta, replace

/* Panel C : 91-99 */
use temp2.dta, clear
/* Balanced sample: treated in first & second period */
keep if an==1991 | an==1999
keep if (ftreat==1 & an==1991 & !missing(F.an)) | (L.ftreat==1 & an==1999)
tab an
gen nind="C"+nninouv
egen btime=group(an)
save tempC.dta, replace

/* Panel D : 99-07 */
use temp2.dta, clear
/* Balanced sample: treated in first & second period */
keep if an==1999 | an==2007
keep if (ftreat==1 & an==1999 & !missing(F.an)) | (L.ftreat==1 & an==2007)
tab an
gen nind="D"+nninouv
egen btime=group(an)
save tempD.dta, replace

/* Merge all balanced sample */
use tempA.dta, clear
append using tempB.dta
append using tempC.dta
append using tempD.dta
save temp3.dta, replace

/* keep the original location */
drop if btime==2
keep nind ze zer depr dep
rename ze oze
rename zer ozer
rename depr odepr
rename dep odep
save oze.dta, replace

/* actual location: start with a=> aze, etc... original location: ze */
use temp3.dta, clear
drop _merge
joinby nind using oze.dta, unm(m)
rename ze aze
rename zer azer
rename dep adep
rename depr adepr
rename oze ze
rename ozer zer
rename odepr depr
rename odep dep
drop _merge

/**********************************************************/
/* Merge with immigration shocks in the original location */
/**********************************************************/
joinby ze an using "ImmPanelRevis19\Donnees\pit\instru2017R21.dta", unm(m)
tab _merge
drop _merge


/*********************/
/* change in outcome */
/*********************/
egen indiv2=group(nind)
xtset indiv2 btime
sort indiv2 btime

/**************/
/* occupation */
/**************/
/* Changes in task intensity */
gen dzbasman=D.zbasman 
gen dzbascom=D.zbascom 

/* fixed effects */
xi, pre(Y_) i.an*lsconst i.an*lstrad i.an*lsntrad i.an*lzepop

/* location shifter */
gen lshift=(ze!=aze)
joinby aze an using tempAZE.dta, unm(m)
/* change in share of immigrants in the commuting zone */
gen dzeimmig=azeimmig-zeimmig
/* change in average wage in the commuting zone */
gen damlsnd=D.amlsnd
/* change in total population in the commuting zone */
gen dazepop=ln(azepop)-ln(L.azepop)

/* housing costs */
drop _merge
joinby aze using "ImmPanelRevis19\Donnees\Loyer\cloyer2.dta", unm(m)
drop _merge
joinby aze an using "ImmPanelRevis19\Donnees\Loyer\ipc_Local.dta", unm(m)
drop _merge

/* wage adjusted for housing costs */
gen lsnd6=ln((sn/dp)*(100/ipcl6))
gen lsnd20=ln((sn/dp)*(100/ipcl20))
gen lsnd30=ln((sn/dp)*(100/ipcl30))

gen hcost=cloyer2-L.cloyer2
gen lhcost=ln(cloyer2)-ln(L.cloyer2)
gen liloy=ln(iloy)-ln(L.iloy)

gen elim=(ze=="9306" | ze=="9109" | ze=="9110" | ze=="9116" | ///
ze=="8307" | ze=="7206" | ze=="5214" | ze=="4101" | ze=="4111" | ze=="8306")
tab elim

/* eliminate small CZ */
drop if ze=="9306" | ze=="9109" | ze=="9110" | ze=="9116" | ze=="8307" | ze=="7206" | ze=="5214" 
drop if ze=="4101" | ze=="4111" | ze=="8306"

/* elimine outliers de l'age */
drop if btime==2 & age<32
drop if btime==2 & age>59

drop if btime==1 & missing(F.age)

/* fixed effects age x an */
xi i.age*i.an , pre(F_)

/* merge technicians & clerks */
gen tecemp2=(tec==1 | emp2==1)

save "ImmPanelRevis19\Donnees\select\finalmw.dta", replace

use "ImmPanelRevis19\Donnees\select\finalmw.dta", clear
keep if homme==1
save "ImmPanelRevis19\Donnees\select\final.dta", replace
use "ImmPanelRevis19\Donnees\select\finalmw.dta", clear
keep if homme==0
save "ImmPanelRevis19\Donnees\select\finalw.dta", replace
